﻿using Microsoft.EntityFrameworkCore;
using Super.Admin.EFCore;
using Super.Admin.Entities;
using Super.Admin.IServices;

namespace Super.Admin.Services
{
    /// <summary>
    /// 部门管理服务实现类
    /// </summary>
    public class DepartmentService : BaseService, IDepartmentService
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="context">操作的数据库上下文</param>
        public DepartmentService(SuperDataBaseDbContext context) : base(context) { }
        /// <summary>
        /// 获取所有部门
        /// </summary>
        /// <returns>部门集合</returns>
        public IReadOnlyCollection<DepartmentEntity> GetDepartments()
        {
            return Query<DepartmentEntity>(d => true).Include(d => d.Users).Include(d => d.PersonInChargeModel).ToList();
        }
        /// <summary>
        /// 创建部门
        /// </summary>
        /// <param name="department">需要创建的部门</param>
        public void CreateDepartment(DepartmentEntity deptEntity)
        {
            deptEntity.CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            deptEntity.UpdateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            deptEntity.Id = Query<DepartmentEntity>(d => true).Max(d => d.Id) + 1;
            Insert<DepartmentEntity>(deptEntity);
            base.Commit(); // 提交事务
        }
        /// <summary>
        /// 修改部门
        /// </summary>
        /// <param name="department">需要修改的部门</param>
        public void UpdateDepartment(DepartmentEntity deptEntity)
        {
            deptEntity.UpdateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            Update<DepartmentEntity>(deptEntity);
            base.Commit(); // 提交事务
        }
        /// <summary>
        /// 删除部门
        /// </summary>
        /// <param name="departmentId">需要删除的部门Id</param>
        public void DeleteDepartment(int id)
        {
            Delete<DepartmentEntity>(id);
            base.Commit(); // 提交事务
        }
    }
}
